<?php

namespace addons\crontab\model;

use think\Model;
use think\Db;
use app\admin\model\inventory\Temporary;

//其他入库单
class RdRecord08 extends Model
{
    protected $connection = "database.inc";
    protected $table = "RdRecord08";
    
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    // 定义时间戳字段名
    protected $createTime = 'dnmaketime';
    protected $updateTime = 'dnmodifytime';
    // 定义字段类型
    protected $type = [
    ];
    // 追加属性
    protected $append = [
    ];
    
    /*
     * 新增数据（表头）,返回表头对象,3其他入、8调拨入
     *   */
    public function insertMain($data,$pc_order_type = 3){
        if(!$data){
            exception('RdRecord08表头为空');
        }
        
        $model =  self::where('cCode',$data['code'])->find();
        if(!$model){
            //u8用户名
            $u8username = Db::connect('database.sec')->table('UA_User_Ex')->where('cUser_Id',$data['userid'])->value('cUser_Name');
            if(!$u8username){
                exception('RdRecord08找不多对应u8用户');
            }
            
            //默认其他入库    还有调拨入库、转换入库
            $cBusType = '其他入库';
            $cSource = '库存';
            $cRdCode = 19;
            if($pc_order_type == 8){
                $cBusType = '调拨入库';
                $cSource = '调拨';
                $cRdCode = '';
            }
            
            //表头
            $model = self::allowField(true)->create([
                'ID'=>Crontab::getAutoIDByTname('RdRecord08'),//收发记录主表标识   表头id 自增
                'bRdFlag'=> 1,//收发标志
                'cVouchType' => '08',//单据类型编码
                'cBusType' => $cBusType,
                'cSource' => $cSource,
                'cBusCode' => $data['code'],//对应业务单号 调拨单就写调拨单号
                'cWhCode' => $data['wh_code'],
                'dDate'=>$data['order_date'],
                'cCode'=>$data['code'],//这里得用u8的单据号生成规则IC002201000175
                'cRdCode' => $cRdCode,
                'cMemo' => isset($data['remark'])?$data['remark']:'',//表头备注
                'cMaker' =>$u8username,//制单人
                'VT_ID' => 89,
                'dnmaketime' => date('Y-m-d',time()), //制单日期
            ]);
        }else{
            $model->dnmodifytime = date('Y-m-d',time());
            $model->save();
        }
        return $model;
    }
    
    //回写u8存储过程
    /*
     * 传入参数，表头流水号ID，账套编号001、999
     * 返回状态0成功 、1失败，和结果
     *   */
    public function synDataToU8($main_id,$dbname = '001'){
        if(!$main_id){
            exception('表头ID不能为空');
        }
        //还没有
        return ;
        $sql = "SET NOCOUNT ON;declare @p6 varchar(1000)
        declare @p7 int
        exec sily_generator_RdRecord01_sync '{$main_id}','{$dbname}',@p6 output,@p7 output
        select @p6 as result ,@p7 as status";
        
        $arr = Db::connect('database.inc')->query($sql);
        
        if(!isset($arr[0]['status'])){
            exception('回写u8失败');
        }
        
        if(!$arr[0]['status']){
            exception('回写u8失败:'.$arr[0]['result']);
        }else{
            //回写成功，返回u8单号
            $temporaryModel = new Temporary();
            $temporary = $temporaryModel->save([
                'u8_code' => $arr[0]['result']
            ],[
                'mid_rdrecord_id'=>$main_id
            ]);
        }
    }
}
